﻿OB.Emes_ActionButton = OB.Emes_ActionButton || {};

//作业订单数据采集
OB.Emes_ActionButton.JobOrderAction = function(params, win) {
	var grid = win.view.viewGrid;
	var record = grid.getSelectedRecord();
	
	OB.Emes_ActionButton.dataCollectionOpenWindow = win;
	OB.Emes_ActionButton.orderAssignWindow = isc.EMESO_DataCollectionWindow.create(record);
	OB.Emes_ActionButton.orderAssignWindow.show();
};
var DataCollectionView_Init_Data = {
	title: "生产数据采集",
	width: '80%',
	height: '80%',
	align: 'center',
	defaultLayoutAlign: 'center',
	allItem: [],
	submit: function(isForce){
		var _self = this;
		var scrapWeight = dataCollectionInfo.getValue('scrapWeight');
		var scrapQty = dataCollectionInfo.getValue('scrapQty');
		
		if(isNaN(scrapWeight)){
			isc.showMessage("碎片重量输入错误！", isc.OBMessageBar.TYPE_ERROR);
			return;
		}
		
		var form2Group = this.dataCollectionInfo.getField("group").itemIds;
		var form3Group = this.addCollectionItem.getField("group").itemIds;
		var arr = [];
		
		for(var i = 0 ; i < form2Group.length ; i ++){
			if(form2Group[i].indexOf("_item_") == 0 ){
				var item = this.dataCollectionInfo.getField(form2Group[i]);
				var obj = {};
				obj.dataType = item.dataType;
				obj.value = item.getValue();
				obj.id = item.paramId;
				obj.seqNo = item.seqNo;
				arr.push(obj);
			}
		}
		
		for(var i = 0 ; i < form3Group.length ; i ++){
			if(form3Group[i].indexOf("_itemTextValue_") == 0 ){
				var itemValue = _self.addCollectionItem.getField(form3Group[i]);
				var obj = {};
				obj.dataType = itemValue.dataType;
				obj.value = itemValue.getValue();
				obj.id = itemValue.paramId;
				arr.push(obj);
			}
		}
		//校验数据采集项目
		for(var i = 0 ; i < arr.length ; i ++){
			if((arr[i].value == null || arr[i].value == "") && arr[i].dataType != 'SELECTOR'){
				isc.showMessage("采集选项值不能为空", isc.OBMessageBar.TYPE_ERROR);
				return;
			}
			if(arr[i].dataType == 'NUMBER' && !/^[0-9]+$/.test(arr[i].value)){
				isc.showMessage("采集选项为数值的请录入数字", isc.OBMessageBar.TYPE_ERROR);
				return;
			}
		}
		
		if (this.submitLoading) {
			return;
		}
		isc.confirm("确定提交数据采集完工？", function(bool){
			if (bool) {
				var requestData = {
					method: "submit",
					arr: arr,
					orderId: _self.orderId,
					processId: processId,
					scrapWeight: scrapWeight,
					scrapQty: scrapQty,
					description: _self.dataCollectionInfo.getValue('description')
				};
				_self.submitLoading = true;
				OB.RemoteCallManager.call("com.mes.handler.collection.EmesProcessDataInfoHandler", requestData, {},
					function(response, data, request){
						_self.submitLoading = false;
						if(data.result){
							_self.clearForm();
							_self.initPageInfo();
							
							if (OB.Emes_ActionButton.dataCollectionOpenWindow) {
								OB.Emes_ActionButton.dataCollectionOpenWindow.refresh();
							}
							isc.showMessage("提交成功！", isc.OBMessageBar.TYPE_INFO);
						}else{
							isc.showMessage(data.text, isc.OBMessageBar.TYPE_ERROR);
						}
					}
				);
			}
		});
	},
	initWidget: function () {
		this.height = document.body.clientHeight * 0.8;
		this.width = document.body.clientWidth * 0.8;
		//OB控件 追加	addField or addItem	会有样式和事件找不到的问题
		this.createOrderInfoForm();
		this.createForm();
		
		var pageLayout = isc.VLayout.create({
			width: '100%',
			height: this.height,
			layoutMargin : 5,
			membersMargin : 10,
			members : [
				this.jobOrderInfoLayout,
				this.dataCollectionInfo,
				this.addCollectionItem,
				this.buttonLayout
			]
		});
		
		this.addMembers(pageLayout);
		this.Super("initWidget", arguments);
		
		this.initPageInfo();
	},
	initPageInfo: function(){
		var _self = this;
		var requestData = {method: "initData"};
		OB.RemoteCallManager.call("com.mes.handler.collection.EmesProcessDataInfoHandler",requestData,{},function(response, data, request){
			_self.clearForm();
			if(data && data.result){
				_self.processId = data.processId;
				_self.setFieldValue(_self.orderProcessForm.getField("name"), data.name);
				
				_self.allItem = data.allItem;
				
				if(data.dcItem){
					_self.appendDCItemForm(data.dcItem, readOnly);
				}
				if (data.allOrder) {
					var list = data.allOrder;
					var datas = {};
					for (var i = 0;i < list.length;i ++) {
						datas[list[i].id] = list[i].name;
					}
					_self.orderProcessForm.getField("manuOrderId").setValueMap(datas);
				}
				if (data.processList) {
					var list = data.processList;
					var datas = {};
					for (var i = 0;i < list.length;i ++) {
						datas[list[i].id] = list[i].name;
					}
					_self.orderProcessForm.getField("processId").setValueMap(datas);
				}
			} else {
				isc.showMessage(data.text, isc.OBMessageBar.TYPE_INFO);
			}
		});
	},
	getOrderInfo: function(orderId){
		var requestData = {orderId: orderId, method: "getOrderInfo"};
		OB.RemoteCallManager.call("com.mes.handler.collection.EmesProcessDataInfoHandler", requestData, {}, function(response, data, request){
			if(data && data.result){
				if (data.orderInfo) {
					_self.setValue(data, orderInfoForm);
				}
			} else {
				isc.showMessage(data.text, isc.OBMessageBar.TYPE_INFO);
			}
		});
	},
	createOrderInfoForm: function() {
		var _self = this;
		this.orderProcessForm = isc.DynamicForm.create({
			ID: "orderProcessForm",
			width: 700,
			numCols : 6,
			fields: [
				{width: '*',type: 'select', title: '生产订单',name: 'manuOrderId', canEdit: true, startRow: false, 
					onChange: function(newValue, orderValue) {
						_self.getOrderInfo(newValue);
					}
				},
				{width: '*',type: 'select', title: '当前工序',name: 'processId', canEdit: true, startRow: false},
				{width: '*',type: 'text', title: '数据采集人',name: 'userName', canEdit: false, startRow: false, readOnlyDisplay: "static"}
			]
		});
		this.orderInfoForm = isc.DynamicForm.create({
			ID: "orderInfoForm",
			width: '100%',
			numCols : 6,
			fields : [
				{width: '*',name:'group',type: 'OBSectionItem',defaultValue:'订单信息',sectionExpanded: true,colSpan: 6,itemIds: 
					['productCode','productName','dateForm','dateTo','qty','qtyStart','canQtyStart', 'orderDescription']},
					
				{width: '*',type: 'text', title:'物料编号',	name: 'productCode',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text', title:'物料名称',	name: 'productName',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text', title:'计划开始日期',name: 'dateForm',		canEdit:false, startRow: true, readOnlyDisplay: "static"},
				{width: '*',type: 'text', title:'计划结束日期',name: 'dateTo',		canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'integer', title:'计划数量', name: 'qty',		canEdit:false, startRow: true, readOnlyDisplay: "static"},
				{width: '*',type: 'integer', title:'已完成数量',	name: 'qtyStart',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'integer', title:'剩余完成数量',name: 'canQtyStart',canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'textarea',title:'备注', name: 'orderDescription', width: '100%', height: 20, colSpan: 6, canEdit: false, startRow: true, readOnlyDisplay: "static"}
			]
		});
		
		this.createFileButton();
		
		this.jobOrderInfoLayout = isc.HLayout.create({
			width: '100%',
			layoutMargin: 0,
			memberMargin: 5,
			members:[
			 	isc.VLayout.create({
					width: '75%',
					layoutMargin: 0,
					memberMargin: 0,
					align: 'left',
					defaultLayoutAlign: 'left',
					members:[
						this.orderProcessForm,
					 	this.orderInfoForm
					]
				}),
			 	isc.VLayout.create({
					width: '25%',
					layoutMargin: 15,
					memberMargin: 30,
					align: 'left',
					defaultLayoutAlign: 'left',
					members:[
						this.processStandardBtn,
						this.productPictureBtn,
						this.jobInstructionsBtn
					]
				})
			]
		});
	},
	createFileButton: function() {
		this.processStandardBtn = isc.OBFormButton.create({
			ID:'processStandardBtn',
			title : '工艺标准',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewDrawingsOrderHandler",
					{processId: _self.processId},
					{},
					function(response, data, request) {
						if(data.flag){
							isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
						}else{
							top.isc.showMessage("该作业订单工序未上传工序标准。",top.isc.OBMessageBar.TYPE_SUCCESS);
						}
					}
				);
			}
		});
		OB.Emes_ActionButton.MyRefresh=function(){
			b.refresh();
		}
		this.productPictureBtn = isc.OBFormButton.create({
			ID:'productPictureBtn',
			title : '产品图纸',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewmaterialOrderHandler",
					{processId: _self.processId},
					{},
					function(response, data, request) {
						if(data.flag){
							isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
						}else{
							isc.showMessage(data.message);
						}
					}
				);
			}
		});
		this.jobInstructionsBtn = isc.OBFormButton.create({
			ID:'jobInstructionsBtn',
			title : '作业指导书',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewprocessOrderHandler",
					{processId: _self.processId},
					{},
					function(response, data, request) {
						if(data.flag){
							isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
						}else{
							isc.showMessage(data.message);
						}
					}
				);
			}
		});
	},
	createForm: function() {
		var _self = this;
		this.dataCollectionInfo = isc.DynamicForm.create({
			ID: "dataCollectionInfo",
			numCols : 8,
			fields : [
				{type: 'OBSectionItem', name: 'group', defaultValue: '录入采集信息',sectionExpanded: true,itemIds: 
					['qtyDone','scrapWeight','oneWeight','codeEnd', 'description'], colSpan: 8},
				{width: '*',type: 'integer', title:'完工数量',	name: 'qtyDone', startRow: false, canEdit: false, readOnlyDisplay: "static"},
				{width: '*',type: 'integer', title:'合格数量',	name: 'qtyOk', startRow: false, canEdit: false, readOnlyDisplay: "static"},
				{width: '*',type: 'float', title: '碎片重量', 	name: 'scrapWeight', startRow: true, canEdit: true,
					onChange: function() {
						
					}
				},
				{width: '*',type: 'integer', title:'碎片数量',	name: 'scrapQty', startRow: false, canEdit: false},
				{width: '*',type: 'float', title:'硅片重量标准',	name: 'oneWeight',startRow: false, canEdit: false, readOnlyDisplay: "static"},
				{width: '*',type: 'textarea', title:'备注', colSpan: 4, name:'description', width: 450, height: 40, startRow: true, canEdit: true}
			]
		});
		this.addCollectionItem = isc.DynamicForm.create({
			ID: "addCollectionItem",
			numCols : 8,
			fields : [
				{type: 'OBSectionItem',name:'group',defaultValue:'追加采集项目',sectionExpanded: true,itemIds: ['btnAdd'],colSpan:8},
				{width: '70',type: 'button',title: '新增采集项',	 name: 'btnAdd',click: 
					function() {
						_self.appendOtherItems({name:"kkk21", itemDatatype: "2", title: "选项"})
					}
				}
			]
		});
		this.submitOkBtn = isc.OBFormButton.create({
			ID: 'submitOkBtn',
			title: '提交',
			action: function() {
				_self.submit(false);
			}
		});
		
		this.submitDelBtn = isc.OBFormButton.create({
			ID: 'submitDelBtn',
			title: '重置',
			action: function() {
				_self.clearForm();
				OB.Emes_ActionButton.orderAssignWindow.closeClick();
			}
		});
		this.buttonLayout = isc.HLayout.create({
			width: '100%',
			layoutMargin: 15,
			memberMargin: 30,
			align: 'center',
			defaultLayoutAlign: 'center',
			members:[
		 		this.submitOkBtn,
				this.submitDelBtn
			]
		});
	},
	clearForm: function(){
		this.clearDCItemForm();//清除表单二选项
		this.clearOtherItemForm();//清除表单三的选项
		
		this.setValue({}, orderInfoForm);
	},
	setValue: function(data, form){
		var itemIds = form.getFields();
		for(var i = 0 ; i < itemIds.length; i ++){
			var column = itemIds[i];
			this.setFieldValue(form.getField(column.name), data[column.name]);
		}
	},
	setFieldValue: function(field, value) {
		if (field.readOnlyDisplay = "static") {
			field.setReadOnlyDisplay("");
			field.setValue(value);
			field.setReadOnlyDisplay("static");
		} else {
			field.setValue(value);
		}
	},
	index:0,
	getIndex:function(){
		return this.index++;
	},
	appendDCItemForm: function(data,readOnly){
		var group = dataCollectionInfo.getItem("group").itemIds;
		var startRow = false;
		for(var i = 0 ; i < data.length ; i ++){
			startRow = (i % 2) == 0;
			var name = "_item_"+this.getIndex();
			group.push(name);
			if(data[i].type == "NUMBER"){//数字
				dataCollectionInfo.addField({
					seqNo: data[i].seqNo, paramId: data[i].id, dataType: data[i].type, value: data[i].value,
					type: 'integer', title: data[i].name + "-数值", name: name, width: '*', length: 5, startRow: startRow
				});
			}else if(data[i].type == "SELECTOR"){//选项
				dataCollectionInfo.addField({
					seqNo: data[i].seqNo, paramId: data[i].id, dataType: data[i].type, value: data[i].value,
					type: 'select', title: data[i].name + "-选项", name: name, width: '*',  startRow: startRow, valueMap: data[i].child
				});
			}else if(data[i].type == "TEXT"){//描述
				dataCollectionInfo.addField({
					seqNo: data[i].seqNo, paramId: data[i].id, dataType: data[i].type, value: data[i].value, 
					type: 'text', title: data[i].name + "-文本", name: name, width: '*', length: 200, startRow: startRow, required: false});
			}
			if(readOnly){
				var column = dataCollectionInfo.getField(name);
				column.canEdit = false;
				column.readOnlyDisplay = "static";
			}
		}
	},
	clearDCItemForm: function(){
		var group = dataCollectionInfo.getItem("group").itemIds;
		for(var i = group.size()-1 ; i >= 0 ; i --){
			if(group[i].indexOf("_item_") == 0 ){
				dataCollectionInfo.removeItem(dataCollectionInfo.getItem(group[i]));
				group.remove(group[i]);
			}
		}
	},
	appendOtherItems: function(data){
		var group = addCollectionItem.getItem("group").itemIds;
		var index = this.getIndex();
		var itemName = "_item_" + index;
		var itemTextValueName = "_itemTextValue_"+index;
		var itemButtonName = "_itemBtnDel_" + index;
		group.push(itemTextValueName);
		group.push(itemButtonName);
		group.push(itemName);//追缴到3个倍数 位置不能修改
		
		//数据采集项目
		var item = {type: 'select',title:'采集项目',name:itemName,width: '*',startRow: true,valueMap: {}, addUnknownValues: false, changed: function(){
			OB.RemoteCallManager.call("com.mes.handler.collection.EmesProcessDataInfoHandler",
				{method: "getItemData", itemId: this.getValue()},{},
				function(response, data, request) {
					var item = data.item;
					var position = 0;
					for(var i = 0 ; i < group.length ; i ++){
						if(itemName == group[i]){
							position = i;
						}
					}
					if(position == 0 ){
						// form 是个表格	 之前加了obsectionitem 和 数据选项下拉框	因此	选项 使用表格占位	3/6/9
						//数据选项可以删除 ，位置无法从表单里面或者ITEM来获取	所以通过组 组本来需要管理所有控件	默认加入了 btnAdd	所以 数据选项	在 group位置调整到 3 6 9 的倍数
						return;
					}
					addCollectionItem.clearValue(itemTextValueName);
					addCollectionItem.removeItem(position);
					if("NUMBER" == item.type){
						var itemNumberValue = {
							paramId: item.id, dataType: item.type, width: '*', name: itemTextValueName, type: 'integer', title: '数值', length: 5, position: position
						};
						addCollectionItem.addField(itemNumberValue,position);
					}else if("SELECTOR" == item.type){
						var itemChild = {
							paramId: item.id, dataType: item.type, width: '*', name: itemTextValueName, type: 'select', title: "选项",valueMap: item.child, addUnknownValues: false, position: position
						};
						addCollectionItem.addField(itemChild,position);
					}else{//默认 3 或者 其他都是文本框
						var itemTextValue = {
							paramId: item.id, dataType: item.type, width: '*', name: itemTextValueName, type: 'text', title:'描述', position: position, length: 200
						};
						addCollectionItem.addField(itemTextValue, position);
					}
				}
			);
		}};
		item.valueMap = this.allItem;
		
		var itemTextValue = {dataType: 3,width: '*',type: 'text',name:itemTextValueName,length: 200, title:'描述'};
		var itemButton = {
			name:itemButtonName,
			width: '50',
			type: 'button',
			title: '-',
			startRow: false,
			removeName: [itemName,itemTextValueName,itemButtonName],
			click: function(){
				//从组中移除
				for(var i = 0 ; i <	this.removeName.length ; i ++){
					var name = this.removeName[i];
					addCollectionItem.removeItem(addCollectionItem.getItem(name));
					group.remove(name);
				}
			}
		}
		
		addCollectionItem.addField(item);
		addCollectionItem.addField(itemTextValue);
		addCollectionItem.addField(itemButton);
	},
	clearOtherItemForm: function(){
		var group = addCollectionItem.getItem("group").itemIds;
		for(var i = group.size()-1 ; i >= 0 ; i --){
			var startWith = group[i].indexOf("_item");
			if(startWith == 0){
				addCollectionItem.removeItem(addCollectionItem.getItem(group[i]));
				group.remove(group[i]);
			}
		}
	}
};

isc.defineClass("EMESO_DataCollectionView", isc.OBBaseView).addProperties(DataCollectionView_Init_Data);
isc.defineClass("EMESO_DataCollectionWindow", isc.OBPopup).addProperties(DataCollectionView_Init_Data);
